home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cserial.zip / VT100.C < prev    next >
C/C++ Source or Header  |  1990-04-04  |  22KB  |  1,223 lines

  1. /*
  2.  *                               VT100.C
  3.  *
  4.  *                           Written for the
  5.  *
  6.  *                              Datalight
  7.  *                           Microsoft V 5.x
  8.  *                                TurboC
  9.  *                                  &
  10.  *                               Zortech
  11.  *
  12.  *                             C Compilers
  13.  *
  14.  *            Copyright (c) John Birchfield 1987, 1988, 1989
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <process.h>
  19. #include <ctype.h>
  20. #include "_kb.h"
  21. #include "8250xon.h"
  22. #include "screen.h"
  23. #include "vt100.h"
  24. #if (!defined (TRUE))
  25. #    define TRUE  (1)
  26. #    define FALSE (0)
  27. #endif
  28.  
  29. char Duplex [2]    = { 'F', 0 },
  30.      RubOut [2]    = { 255, 0 },
  31.      BackSpace [2] = { 8,   0 },
  32.      Cmask     [2] = {127,  0 },
  33.      CrLf      [2] = {  0,  0 };
  34.  
  35.  
  36. #if (defined (STANDALONE))
  37. #include "options.h"
  38. void print_screen_header (void);
  39.  
  40. main (argc, argv)
  41. int argc;
  42. char *argv [];
  43. {
  44.     FILE *fp;
  45.     void vt100_driver (), VT100_init ();
  46.     set_options (argc, argv);
  47.     trap_ctrl_break ();
  48.     timer_init ();
  49.     xon8250_init (Port, 4096);
  50.     fputs ("--  VT100 Terminal Emulator --\n", stdout);
  51.     if (Cfg_Str [0])
  52.     {
  53.         xon8250_port_init (Cfg_Str);
  54.         fputs (Opt_Msg, stdout);
  55.     }
  56.     else
  57.     {
  58.         xon8250_port_enable ();
  59.         fputs ("Default Port Settings Used\n", stdout);
  60.     }
  61.     fputs ("Use <Alt>F10 to exit, <Alt>F9 to spawn DOS,"
  62.            " or <Alt>F8 to Send Break\n", stdout);
  63.     screen_init();
  64.     
  65.     print_screen_header ();
  66.     rowcol (23, 0);
  67.     while (xon8250_timed_read (1) != -1)
  68.         ;
  69.     VT100_init ();
  70.     vt100_driver ();
  71.     xon8250_term ((Cfg_Str [0])?1:0);
  72.     timer_term ();
  73.     release_ctrl_break ();
  74. }
  75.  
  76.  
  77.  
  78. void
  79. print_screen_header ()
  80. {
  81.     char save = Scr_ATTR;
  82.     Scr_ATTR = INVERSE;
  83.     rowcol(24,0);
  84.     cleol();
  85.     rowcol (24, 0);
  86.     aputs (INVERSE, "VT100 Emulator");
  87.     Scr_ATTR = save;
  88. }
  89. #endif
  90.  
  91.  
  92.  
  93. /*--------------------------------------------------------------------*
  94.  |               All of the VT100 Specific Stuff Follows              |
  95.  *--------------------------------------------------------------------*/
  96. void    ANSI_MODE (void), CUB (void), CUD (void), CUF (void),
  97.         CUP (void), CUU (void), DA (void), DECSTBM (void), DECSC (void),
  98.         DECALN (void), DECRC (void), DSR (void), ED (void), EL (void),
  99.         RESET (void), RM (void), SGR (void), SM (void), TBC (void),
  100.         escA (void), escB (void), escC (void), escD (void), escE (void),
  101.         escF (void), escG (void), escH (void), escI (void),
  102.         escJ (void), escK (void), escM (void), escY (void), escZ (void);
  103.  
  104. #define    P_MAX         6    /* max no of parameter accumulators */
  105. #define E_BUFF_MAX    11    /* max chars in escape interpreter buffer */
  106.  
  107. static    int        save_row,         save_col,
  108.                 save_attr,        save_crlf,
  109.                 initdone = '\0',
  110.                 tab_stop[80],
  111.                 special_graphics, ansi_mode,
  112.                 appl_mode,        DECKPNM,
  113.                 vt_save_attr,
  114.                 vt_save_row,      vt_save_col,
  115.                 vt_top,              vt_bot,
  116.                 vt_tlim,          vt_blim,
  117.                 vt_row,           vt_col,
  118.                 vt_crlf,          vt_org,
  119.                 vt_wrap,          vt_xoff;
  120.  
  121. char            ansi_cursor_set[]    = "\\eO%c",
  122.                 ansi_cursor_reset[]    = "\\e[%c",
  123.                 vt52_cursor[]        = "\\e%c",
  124.                   vt52_kp[]            = "\\e?%c",
  125.                 ansi_kp[]            = "\\eO%c",
  126.                 *kp_sptr,            *cursor_sptr;
  127.  
  128. void vt_putf(char *,...),
  129.      vtpf_itoa (int),
  130.      vtput_char (char c),
  131.      VT_Put_Scr (char),
  132.      VT100_Cmd (char),
  133.      VT100_KB (char),
  134.      VT100_Out (char);
  135.  
  136. void
  137. vt100_driver () 
  138. {
  139.     int chin, chout;
  140.     do 
  141.     {
  142.         if (!(vt_xoff) && ((chin = xon8250_read ()) != -1))
  143.             if (chin &= *Cmask)
  144.                 VT100_Cmd (chin);
  145.         if ((chout = _kb ()) != -1) 
  146.         {
  147.             switch (chout) 
  148.             {
  149.                 case PF5:
  150.                     tab_stop[vt_col] = TRUE;
  151.                     break;
  152.                 case PF6:
  153.                     tab_stop[vt_col] = FALSE;
  154.                     break;
  155.                 case APF8:
  156.                     xon8250_write_break ();
  157.                     break;
  158.                 case APF9:
  159.                     spawnlp (0, "COMMAND.COM", "COMMAND.COM", (char *) 0);
  160.                     print_screen_header ();
  161.                     vt_row = 23; vt_col = 0;
  162.                     rowcol (vt_row, vt_col);
  163.                     break;
  164.                 case APF10:
  165.                     break;
  166.                 default:
  167.                     if (chout != APF1)
  168.                         VT100_KB (chout);
  169.                     break;
  170.             }
  171.         }
  172.     } while (chout != APF10);
  173. }
  174.  
  175.  
  176.  
  177.  
  178. /*--------------------------- VT100_Cmd () ---------------------------*/
  179. /*
  180.  *
  181.  */
  182. static    int    p_ix          = 0, p_acc [P_MAX] = { 0 };
  183. static char lastchar = '\0';
  184. void
  185. VT100_Cmd (char ch)
  186. {
  187.     if (ch==BS)
  188.     {
  189.         if (vt_col) 
  190.         {
  191.             rowcol (vt_row,--vt_col);
  192.         }
  193.         return;
  194.     }
  195.     if (lastchar && (ch==CAN || ch==SUB))
  196.         goto reset;
  197.     if (ch==DEL)
  198.         return;
  199.  
  200.     switch (lastchar)
  201.     {
  202.         default:
  203.             if (ch==ESC)
  204.             {
  205.                 lastchar = ch;
  206.                 while (p_ix >= 0)
  207.                     p_acc [p_ix--] = 0;
  208.                 p_ix = 0;
  209.                 return;
  210.             }
  211.             VT100_Out (ch);
  212.             return;
  213.         case ESC:
  214.             switch (ch)
  215.             {
  216.                 case ESC: return;
  217.                 case '[':
  218.                 case '(':
  219.                 case ')':
  220.                 case 'Y':
  221.                 case '#': lastchar = ch;   return;
  222.                 case '7': DECSC ();        goto reset;
  223.                 case '8': DECRC ();        goto reset;
  224.                 case '<': ANSI_MODE ();    goto reset;
  225.                 case '>': DECKPNM = FALSE; goto reset;
  226.                 case '=': DECKPNM = TRUE;  goto reset;
  227.                 case 'A': escA  ();        goto reset;
  228.                 case 'B': escB  ();        goto reset;
  229.                 case 'C': escC  ();        goto reset;
  230.                 case 'D': escD  ();        goto reset;
  231.                 case 'E': escE  ();        goto reset;
  232.                 case 'F': escF  ();        goto reset;
  233.                 case 'G': escG  ();        goto reset;
  234.                 case 'H': escH  ();        goto reset;
  235.                 case 'I': escI  ();        goto reset;
  236.                 case 'J': ED ();           goto reset;
  237.                 case 'K': EL ();           goto reset;
  238.                 case 'M': escM  ();        goto reset;
  239.                 case 'Z': escZ  ();        goto reset;
  240.                 case 'c': RESET ();        goto reset;
  241.                 case 'y':                  goto reset;
  242.                 default : VT100_Out (ch);  goto reset;
  243.             }
  244.         case '[':
  245.             switch (ch)
  246.             {
  247.                 case 'A': CUU ();     goto reset;
  248.                 case 'B': CUD ();     goto reset;
  249.                 case 'C': CUF ();     goto reset;
  250.                 case 'D': CUB ();     goto reset;
  251.                 case 'H':
  252.                 case 'f': CUP ();     goto reset;
  253.                 case 'J': ED ();      goto reset;
  254.                 case 'K': EL ();      goto reset;
  255.                 case 'c': DA ();      goto reset;
  256.                 case 'g': TBC ();     goto reset;
  257.                 case 'h': SM ();      goto reset;
  258.                 case 'l': RM ();      goto reset;
  259.                 case 'm': SGR ();     goto reset;
  260.                 case 'n': DSR ();     goto reset;
  261.                 case 'q':             goto reset;
  262.                 case 'r': DECSTBM (); goto reset;
  263.                 case ';': p_ix++;
  264.                 case '?':             return;
  265.                 default:
  266.                     if isdigit (ch)
  267.                     {
  268.                         p_acc [p_ix] *= 10;
  269.                         p_acc [p_ix] += (ch-'0');
  270.                         return;
  271.                     }
  272.                     VT100_Out (ch);
  273.                     goto reset;
  274.             }
  275.         case '(':
  276.         case ')': goto reset;
  277.         case '#':
  278.             if (ch=='8')
  279.                 DECALN ();
  280.             goto reset;
  281.         case 'Y':
  282.             p_acc [p_ix++] = ch - 31;
  283.             if (p_ix < 2)
  284.                 break;
  285.             else
  286.             {
  287.                 CUP ();
  288.                 goto reset;
  289.             }
  290.     }
  291.     return;
  292. reset:
  293.     lastchar = '\0';
  294. }
  295.  
  296.  
  297.  
  298.  
  299. /*--------------------------- VT100_Out () ---------------------------*/
  300. /*
  301.  *
  302.  */
  303. void
  304. VT100_Out (char ch)
  305. {
  306.     switch (ch) 
  307.     {
  308.         case BEL:
  309.             putchar (ch);
  310.             rowcol (vt_row, vt_col);
  311.             break;
  312.         case HT:
  313.             while (vt_col < 79)
  314.                 if (tab_stop[++vt_col])
  315.                     break;
  316.             rowcol(vt_row, vt_col);
  317.             break;
  318.         case DEL:
  319.         case ESC:
  320.             break;
  321.         default:
  322.             VT_Put_Scr (ch);
  323.             break;
  324.     }
  325. }
  326.  
  327.  
  328.  
  329.  
  330. /*------------------------- VT_Put_Scr () -------------------------*/
  331. /*
  332.  *
  333.  */
  334. void
  335. VT_Put_Scr (char ch)
  336. {
  337.     static char vt_wrapped = FALSE;
  338.  
  339.     if (vt_wrapped) 
  340.     {
  341.         if (ch == CR)
  342.             return;
  343.         vt_wrapped = FALSE;
  344.         if (ch == LF)
  345.             return;
  346.     }
  347.     if (vt_row > vt_bot)
  348.     {
  349.         rowcol (vt_row=vt_bot, vt_col);
  350.         scroll_up (1, vt_top, 0, vt_bot, 79);
  351.     }
  352.     switch (ch) 
  353.     {
  354.         case CR:
  355.             vt_col = 0;
  356.             rowcol (vt_row, vt_col);
  357.             if (vt_crlf == FALSE)
  358.                 break;
  359.         case LF:
  360.         case VT:
  361.         case FF:
  362.             if (++vt_row > vt_bot)
  363.                 scroll_up (1, vt_top, 0, vt_row=vt_bot, 79);
  364.             rowcol (vt_row, vt_col);
  365.             break;
  366.         default:
  367.             aput (Scr_ATTR, ch);
  368.             if (++vt_col > 79) 
  369.             {
  370.                 if (!vt_wrap)
  371.                     rowcol (vt_row, --vt_col);
  372.                 else
  373.